import os.path
from pprint import pprint

import requests

try:
    id = '222'
    url = "https://ke.wechess.cn/app/index.php?i=5&c=entry&m=xiangqi_lesson&do=lesson&op=getCatalogList"
    header = {
        'Referer' : 'https://ke.wechess.cn/app/index.php?i=5&c=entry&m=xiangqi_lesson&do=lesson&id=' + 'id',
    }
    body = {
        'id' : id
    }
    response = requests.post(url,data=body,headers=header)

    response.encoding = "utf-8"
    json_dict = response.json()
    pageList = json_dict["data"]

    for index, page in enumerate(pageList, start=0):
        title = page["title"]
        sec = page["section"]
        path = f"e:/{title}"
        if not os.path.exists(path):
            os.mkdir(path)
            for num, temp in enumerate(sec, start=0):
                resp = requests.get(temp["videourl"]).content
                name = temp["title"]
                file = f"e:/{title}/{name}.mp4"

                with open(file, "wb") as f:
                    f.write(resp)
                    print(f"download:{name}")


except Exception as e:
    # 下载失败返回False
    print(e)